<?php
/**
 *   Joomdiet - administrator/components/models/foodvariable.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');

class JoomdietModelFoodvariable extends JModel
{
    var $_id;
    var $_foodid;
    var $_foodvariable;
    var $_data;
    
    function __construct()
    {
        parent::__construct();
        
        try {
            $array = JRequest::getVar('cid', 0, '', 'array');
            $this->setId((int)$array[0]);
        } catch (Exception $e) {
            // do nothing
        }
    }
    
    function _buildQuery()
    {
        $query = 'SELECT * FROM #__joomdiet_Food_variable WHERE id_food='.
            $this->_foodid;
        return $query;
    }
    
    function setId($id)
    {
        $this->_foodid = $id;
        $this->_data = null;
        $this->_foodvariable = null;
    }
    
    function getData()
    {
        if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query);
            if (empty($this->_data)) $this->_data = array();
        }

        return $this->_data;
    }
    
    function loadFoodVariable($variable_id)
    {
        if (empty($this->_foodvariable) and 
            ($this->_foodvariable->id_variable == $variable_id)) {
            $query = 'SELECT * FROM #__joomdiet_Food_variable'.
                ' WHERE id_food='.$this->id_food.' AND'.
                ' id_variable='.$this->id_variable;
            $this->_db->setQuery($query);
            $this->_foodvariable = $this->_db->loadObject();
        }
        
        return $this->_foodvariable;
    }
    
    function insert($food_id, $variable_id, $value = 0)
    {
        $query = 'INSERT INTO #__joomdiet_Food_variable'.
            ' (id_food, id_variable, amount) VALUES'.
            ' ('.$food_id.', '.$variable_id.', '.$value.')';
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function update()
    {
        $query = 'UPDATE #__joomdiet_Food_variable'.
            ' SET amount="'.$this->_foodvariable->amount.'"'.
            ' WHERE id_food='.$this->_foodvariable->id_food.' AND'.
            ' id_variable='.$this->_foodvariable->id_variable;
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function delete($food_id, $variable_id)
    {
        $query = 'DELETE FROM #__joomdiet_Food_variable'.
            ' WHERE id_food='.$food_id.' AND'.
            ' id_variable='.$variable_id;
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function apply($id)
    {
        $query = 'SELECT id_variable FROM #__joomdiet_Food_variable'.
            ' WHERE id_food='.$id;
        $this->_db->setQuery($query);
        $variable_ids = $this->_db->loadResultArray();
        foreach ($variable_ids as $variable_id) {
            $amount = JRequest::getVar('variable'.$variable_id);
            $update = 'UPDATE #__joomdiet_Food_variable'.
                ' SET amount="'.$amount.'"'.
                ' WHERE id_food='.$id.' AND'.
                ' id_variable='.$variable_id;
            $this->_db->setQuery($update);
            $this->_db->query();
        }
    }
}
?>
